/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.figures;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Stereotype;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

public abstract class SeDiSubpartFigure extends Figure implements ILabeledFigure {

    private Color fontColor;
    private Color borderColor;

    private String stereotypesLabel;

    public Rectangle getPreferredBounds(Rectangle suggestedBounds) {
        return suggestedBounds;
    }

    public Color getFontColor() {
        return fontColor;
    }

    public void setFontColor(Color fontColor) {
        this.fontColor = fontColor;
    }

    public Color getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    /**
     * Sets the stereotypesLabel.
     * 
     * @param stereotypesLabel
     *            the stereotypesLabel to set
     */
    public void setStereotypesLabel(String stereotypesLabel) {
        this.stereotypesLabel = stereotypesLabel;
    }

    /**
     * returns the stereotypesLabel.
     * 
     * @return the stereotypesLabel.
     */
    public String getStereotypesLabel() {
        return stereotypesLabel;
    }

    /**
     * Refresh colors.
     * 
     * @param graphElement
     *            the graph element
     */
    public void refreshAll(GraphElement graphElement) {
        // set colors
        // backgroundColor
        if (graphElement.getBackgroundColor() != null) {
            this.setBackgroundColor(PapyrusPlugin.colorManager.get(graphElement
                    .getBackgroundColor()));
        }
        if (graphElement.getFontColor() != null) {
            this.setFontColor(PapyrusPlugin.colorManager.get(graphElement.getFontColor()));
            this.setForegroundColor(PapyrusPlugin.colorManager.get(graphElement.getFontColor()));
        }
        if (graphElement.getBorderColor() != null) {
            this.setBorderColor(PapyrusPlugin.colorManager.get(graphElement.getBorderColor()));
        }
        this.setFont(PapyrusPlugin.fontManager.get(new FontData[] {
            new FontData(graphElement.getFontFamily(), graphElement.getFontSize(), SWT.BOLD)
        }));
        //
        // Stereotypes :
        this.refreshStereotypes(graphElement);

        this.repaint();
    }

    /**
     * Refreshes sterotypes label.
     * 
     * @param graphElement
     */
    public void refreshStereotypes(GraphElement graphElement) {
        if (graphElement.getSemanticModel() instanceof Uml1SemanticModelBridge) {
            Element element =
                    ((Uml1SemanticModelBridge) graphElement.getSemanticModel()).getElement();
            if (!element.getAppliedStereotypes().isEmpty()) {
                StringBuilder stereotypesList = new StringBuilder(PapyrusPlugin.ST_LEFT);
                boolean first = true;
                for (Stereotype stereotype : element.getAppliedStereotypes()) {
                    if (first) {
                        first = false;
                    } else {
                        stereotypesList.append(", ");
                    }
                    stereotypesList.append(stereotype.getName());
                }
                stereotypesList.append(PapyrusPlugin.ST_RIGHT);
                this.setStereotypesLabel(stereotypesList.toString());
            } else {
                this.setStereotypesLabel("");
            }
        }
    }
}
